Imports System.Data

Partial Class Accounting_Payment
    Inherits WebPageCommon

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session.Remove("mode")
        Call SetPageSecurity(divMain)

        Dim sec As New Secure

        With sec
            .ParentPage = Me
            .AddControl(AddNewPayment, Secure.ControlTypes.AddType)
            .AddDataGridColumn(DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
            .AddDataGridColumn(DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)

            .SetPageAccess(Session("UserName"))
        End With

        If Not Page.IsPostBack Then
            Call InitVars()
            'Call Search()
        End If
    End Sub

    Protected Sub Search_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_btn.Click
        Call Search()
    End Sub

    Protected Sub AddNewPayment_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddNewPayment.Click
        Session.Add("pagesource", "~/Accounting/Payment.aspx")
        Response.Redirect("~/Accounting/AddPayment.aspx")
    End Sub

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        Select Case e.CommandName
            Case "view_client"
                Session.Add("Seq_CD", e.Item.Cells(0).Text)
                Session.Add("pagesource", Request.FilePath)
                Session.Add("mode", "view")

                Response.Redirect("~/New_Business/ClientInfo.aspx")
            Case "Edit"
                'Dim x As myVars

                Session.Add("Seq_CD", e.Item.Cells(0).Text)
                Session.Add("CID", e.Item.Cells(1).Text)
                Session.Add("CName", e.Item.Cells(2).Text)
                If Session("CName") = "&nbsp;" Then Session.Remove("CName")
                Session.Add("mode", "edit")
                Session.Add("pagesource", "~/Accounting/Payment.aspx")

                Response.Redirect("~/Accounting/AddPayment.aspx")

            Case "View"
                Session.Add("Seq_CD", e.Item.Cells(0).Text)
                Session.Add("CID", e.Item.Cells(1).Text)
                Session.Add("CName", e.Item.Cells(2).Text)
                If Session("CName") = "&nbsp;" Then Session.Remove("CName")
                Session.Add("mode", "view")
                Session.Add("pagesource", "~/Accounting/Payment.aspx")

                Response.Redirect("~/Accounting/AddPayment.aspx")

            Case "Delete"
                Dim daoARType As New DAOMain.AR_TYPE_PAYMENT_DTL_DAO
                Dim voARType As New DAOMain.AR_TYPE_PAYMENT_DTL_VO

                voARType.PAYMENT_SEQ_CD = e.Item.Cells(2).Text
                daoARType.Delete(voARType)

                Dim daoConfirm As New DAOMain.POLICY_CONFIRM_DTL_DAO
                Dim voConfirm As New DAOMain.POLICY_CONFIRM_DTL_VO

                voConfirm.PAYMENT_SEQ_CD = e.Item.Cells(2).Text
                daoConfirm.Delete(voConfirm)

                Dim daoComm As New DAOMain.COMMISSION_PAYABLE_DTL_DAO
                Dim voComm As New DAOMain.COMMISSION_PAYABLE_DTL_VO

                voComm.PAYMENT_SEQ_CD = e.Item.Cells(2).Text
                daoComm.Delete(voComm)

                Dim voPay As New DAOMain.PAYMENT_DTL_VO
                Dim daoPay As New DAOMain.PAYMENT_DTL_DAO

                voPay.PAYMENT_SEQ_CD = e.Item.Cells(2).Text
                voPay.POLICY_SEQ_CD = e.Item.Cells(0).Text

                daoPay.Delete(voPay)

                Dim voCheck As New DAOMain.PAYMENT_DTL_VO
                Dim daoCheck As New DAOMain.PAYMENT_DTL_DAO

                voCheck.POLICY_PREMIUM_CD = e.Item.Cells(1).Text
                Dim dt As DataTable = daoCheck.Search(voCheck)

                If dt.Rows.Count = 0 Then
                    Dim voPremium As New DAOMain.POLICY_PREMIUM_DTL_VO
                    Dim daoPremium As New DAOMain.POLICY_PREMIUM_DTL_DAO

                    voPremium.POLICY_PREMIUM_CD = e.Item.Cells(1).Text
                    voPremium.POLICY_SEQ_CD = e.Item.Cells(0).Text

                    daoPremium.Delete(voPremium)

                    voPremium = Nothing
                    daoPremium = Nothing
                End If

                Call Search()

                daoConfirm = Nothing
                voConfirm = Nothing
                daoComm = Nothing
                voComm = Nothing
                daoARType = Nothing
                voARType = Nothing
                voPay = Nothing
                daoPay = Nothing

        End Select
    End Sub

    'Protected Sub DataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid.ItemDataBound
    '    Select Case e.Item.ItemType
    '        Case ListItemType.Item, ListItemType.AlternatingItem
    '            e.Item.Cells(8).Text = Format(CDate(e.Item.Cells(8).Text), "M/d/yyyy")
    '    End Select
    'End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        If DataGrid.PageCount >= e.NewPageIndex Then
            DataGrid.CurrentPageIndex = e.NewPageIndex

            DataGrid.DataSource = ViewState("dTable")
            DataGrid.DataBind()
        End If
    End Sub

    Private Sub Search()
        Dim dao As New DAOMain.VW_POLICY_PREMIUM_DTL_DAO
        Dim vo As New DAOMain.VW_POLICY_PREMIUM_DTL_VO

        With vo
            .POLICY_CLIENT_LAST_NAME = "%" & Client_txt.Text & "%"
            .POLICY_CLIENT_FIRST_NAME = "%" & First_Name_txt.Text & "%"
            .POLICY_NO = "%" & Policynumber_txt.Text & "%"
            .PROVIDER_DESC = "%" & Provider_txt.Text & "%"
            .PRDT_DESC = "%" & Product_txt.Text & "%"
            If Trim(Year_txt.Text) <> "" Then .YEAR_DUE = Year_txt.Text
        End With

        Dim dt As DataTable = dao.Search(vo, , True)
        ViewState.Add("dTable", dt)

        results_label.Text = dt.Rows.Count
        DataGrid.CurrentPageIndex = 0
        DataGrid.DataSource = ViewState("dTable")
        DataGrid.DataBind()

        dao = Nothing
        vo = Nothing
    End Sub
End Class
